home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / java / threads / classes / sortitem.java < prev    next >
Encoding:
Java Source  |  1996-02-26  |  6.1 KB  |  252 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * @(#)SortItem.java    1.17 95/04/10 James Gosling
  19.  *
  20.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  21.  *
  22.  * Permission to use, copy, modify, and distribute this software
  23.  * and its documentation for NON-COMMERCIAL purposes and without
  24.  * fee is hereby granted provided that this copyright notice
  25.  * appears in all copies. Please refer to the file "copyright.html"
  26.  * for further important copyright and licensing information.
  27.  *
  28.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  29.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  30.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  31.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  32.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  33.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  34.  */
  35.  
  36. import awt.*;
  37. import browser.NameServer;
  38. import java.io.InputStream;
  39. import java.util.Hashtable;
  40. import net.www.html.*;
  41. import browser.Applet;
  42.  
  43. /**
  44.  * A simple applet class to demonstrate a sort algorithm.
  45.  * You can specify a sorting algorithm using the "alg"
  46.  * attribyte. When you click on the applet, a thread is
  47.  * forked which animates the sorting algorithm.
  48.  *
  49.  * @author James Gosling
  50.  * @version     1.17, 10 Apr 1995
  51.  */
  52. public
  53. class SortItem extends Applet implements Runnable {
  54.     /**
  55.      * The thread that is sorting (or null).
  56.      */
  57.     private Thread kicker;
  58.  
  59.     /**
  60.      * The array that is being sorted.
  61.      */
  62.     int arr[];
  63.  
  64.     /**
  65.      * The high water mark.
  66.      */
  67.     int h1 = -1;
  68.  
  69.     /**
  70.      * The low water mark.
  71.      */
  72.     int h2 = -1;
  73.  
  74.     /**
  75.      * The name of the algorithm.
  76.      */
  77.     String algName;
  78.  
  79.     /**
  80.      * The sorting algorithm (or null).
  81.      */
  82.     SortAlgorithm algorithm;
  83.  
  84.     /**
  85.      * Fill the array with random numbers from 0..n-1.
  86.      */
  87.     void scramble() {
  88.     int a[] = new int[height / 2];
  89.     double f = width / (double) a.length;
  90.     for (int i = a.length; --i >= 0;) {
  91.         a[i] = (int)(i * f);
  92.     }
  93.     for (int i = a.length; --i >= 0;) {
  94.         int j = (int)(i * Math.random());
  95.         int t = a[i];
  96.         a[i] = a[j];
  97.         a[j] = t;
  98.     }
  99.     arr = a;
  100.     }
  101.  
  102.     /**
  103.      * Pause a while.
  104.      * @see SortAlgorithm
  105.      */
  106.     void pause() {
  107.     pause(-1, -1);
  108.     }
  109.  
  110.     /**
  111.      * Pause a while, and draw the high water mark.
  112.      * @see SortAlgorithm
  113.      */
  114.     void pause(int H1) {
  115.     pause(H1, -1);
  116.     }
  117.  
  118.     /**
  119.      * Pause a while, and draw the low&high water marks.
  120.      * @see SortAlgorithm
  121.      */
  122.     void pause(int H1, int H2) {
  123.     h1 = H1;
  124.     h2 = H2;
  125.     if (kicker != null) {
  126.         repaint();
  127.     }
  128.     Thread.sleep(20);
  129.     }
  130.  
  131.     /**
  132.      * Initialize the applet.
  133.      */
  134.     public void init() {
  135.     resize(100, 100);
  136.  
  137.     String at = getAttribute("alg");
  138.     if (at == null) {
  139.         at = "BubbleSort";
  140.     }
  141.  
  142.     algName = at + "Algorithm";
  143.     scramble();
  144.     }
  145.  
  146.     /**
  147.      * Paint the array of numbers as a list
  148.      * of horizontal lines of varying lenghts.
  149.      */
  150.     public void paint(Graphics g) {
  151.     int a[] = arr;
  152.     int y = height - 1;
  153.  
  154.     // Erase old lines
  155.     g.setForeground(Color.lightGray);
  156.     for (int i = a.length; --i >= 0; y -= 2) {
  157.         g.drawLine(arr[i], y, width, y);
  158.     }
  159.  
  160.     // Draw new lines
  161.     g.setForeground(Color.black);
  162.     y = height - 1;
  163.     for (int i = a.length; --i >= 0; y -= 2) {
  164.         g.drawLine(0, y, arr[i], y);
  165.     }
  166.  
  167.     if (h1 >= 0) {
  168.         g.setForeground(Color.red);
  169.         y = h1 * 2 + 1;
  170.         g.drawLine(0, y, width, y);
  171.     }
  172.     if (h2 >= 0) {
  173.         g.setForeground(Color.blue);
  174.         y = h2 * 2 + 1;
  175.         g.drawLine(0, y, width, y);
  176.     }
  177.     }
  178.  
  179.     /**
  180.      * Update without erasing the background.
  181.      */
  182.     public void update(Graphics g) {
  183.     paint(g);
  184.     }
  185.  
  186.     /**
  187.      * Run the sorting algorithm. This method is
  188.      * called by class Thread once the sorting algorithm
  189.      * is started.
  190.      * @see java.lang.Thread#run
  191.      * @see SortItem#mouseUp
  192.      */
  193.     public void run() {
  194.     try {
  195.         if (algorithm == null) {
  196.         algorithm = (SortAlgorithm)new(algName);
  197.         algorithm.setParent(this);
  198.         }
  199.         algorithm.init();
  200.         algorithm.sort(arr);
  201.     } catch(Object e) {
  202.     }
  203.     }
  204.  
  205.     /**
  206.      * Stop the applet. Kill any sorting algorithm that
  207.      * is still sorting.
  208.      */
  209.     public synchronized void stop() {
  210.     if (kicker != null) {
  211.             try {
  212.         kicker.stop();
  213.             } catch (IllegalStateException e) {
  214.                 // ignore this exception
  215.             }
  216.         kicker = null;
  217.     }
  218.     if (algorithm != null){
  219.             try {
  220.         algorithm.stop();
  221.             } catch (IllegalStateException e) {
  222.                 // ignore this exception
  223.             }
  224.     }
  225.     }
  226.  
  227.  
  228.     /**
  229.      * For a Thread to actually do the sorting. This routine makes
  230.      * sure we do not simultaneously start several sorts if the user
  231.      * repeatedly clicks on the sort item.  It needs to be
  232.      * synchronoized with the stop() method because they both
  233.      * manipulate the common kicker variable.
  234.      */
  235.     private synchronized void startSort() {
  236.     if (kicker == null || !kicker.isAlive()) {
  237.         scramble();
  238.         repaint();
  239.         kicker = new Thread(this);
  240.         kicker.start();
  241.     }
  242.     }
  243.  
  244.  
  245.     /**
  246.      * The user clicked in the applet. Start the clock!
  247.      */
  248.     public void mouseUp(int x, int y) {
  249.     startSort();
  250.     }
  251. }
  252.